/**
 * Company : Thai Informatic System Co., Ltd.
 * Created By : Dr.Tree
 * Created On : Feb 14, 2012  3:29:29 PM 
 * e-Mail : chatree@tis.co.th
 * Mobile Phone : (+66) 087-921-5115
 * Package Name : apps.promis.in.common
 * File Name : ProjectAction.java
 * TODO
 */
package apps.promis.in.common;

import java.util.List;

import jpa.entitys.promis.common.Company;
import jpa.entitys.promis.common.CostProject;
import jpa.entitys.promis.common.CostProjectPK;
import jpa.entitys.synchro.staging.MmPlantInb;
import jpa.service.promis.common.ProjectServices;
import jpa.service.synchro.staging.MmPlantInbServices;

import org.apache.log4j.Logger;

import promis.utility.CommonService;
import promis.utility.Utilitys;

import apps.sessionFactory;

/**
 * 
 * @author Chatree Siangklom of Administrator
 * Date =Nov 13, 2013 : 2:36:16 PM 
 * CreateBy=Chatree Siangklom
 * 
 * ProjectAction
 * TODO
 */
public class ProjectAction {
	
	private static final Logger log = Logger.getLogger(ProjectAction.class);
	
	/**
	 * 
	 * Date =Nov 13, 2013 : 2:36:27 PM 
	 * CreateBy=Chatree Siangklom
	 * List<CostProject>
	 * TODO
	 * @return
	 * @throws Exception
	 */
	public List<CostProject> findAll() throws Exception {	
		return new ProjectServices(sessionFactory.CreateEmPromis()).findAll();
	}

	/**
	 * 
	 * Date =Nov 13, 2013 : 2:36:34 PM 
	 * CreateBy=Chatree Siangklom
	 * CostProject
	 * TODO
	 * @param company
	 * @param project
	 * @return
	 * @throws Exception
	 */
	public CostProject FindByKey(String company, String project) throws Exception {
		
		ProjectServices projectSrv = new ProjectServices(sessionFactory.CreateEmPromis());		
		CostProjectPK costProjectPk = new CostProjectPK();
		costProjectPk.setCompany(company);  			log.info(" company = " + company);
		costProjectPk.setProject(project);				log.info(" project = " + project);
		
		return projectSrv.findById(costProjectPk);
	}
	
	/**
	 * 
	 * Date =Nov 13, 2013 : 2:36:37 PM 
	 * CreateBy=Chatree Siangklom
	 * boolean
	 * TODO
	 * @param company
	 * @param project
	 * @return
	 * @throws Exception
	 */
	public boolean ExistingChecked(String company, String project) throws Exception {
		return FindByKey(company, project) != null ? true : false; 
	}
	
	/**
	 * 
	 * Date =Nov 13, 2013 : 2:36:41 PM 
	 * CreateBy=Chatree Siangklom
	 * CostProject
	 * TODO
	 * @param sap_plant_code
	 * @return
	 * @throws Exception
	 */
	public CostProject findPlantByCode(String sap_plant_code) throws Exception{
		ProjectServices projectSrv = new ProjectServices(sessionFactory.CreateEmPromis());
		return  projectSrv.findByPlantCode(sap_plant_code);
	}	
	
	/**
	 * 
	 * Date =Nov 13, 2013 : 2:36:44 PM 
	 * CreateBy=Chatree Siangklom
	 * CostProject
	 * TODO
	 * @param mmPlantInb
	 * @return
	 * @throws Exception
	 */
	public CostProject SaveOrUpdated(MmPlantInb mmPlantInb) throws Exception {		
		try{
			ProjectServices projSrv = new ProjectServices(sessionFactory.CreateEmPromis());
			
			Company company = new CompanyAction().FindBySapCompany(mmPlantInb.getCompanyCode());
			if(CommonService.IsNull(company))
				throw new Exception(" Find not Found Company = " + mmPlantInb.getCompanyCode());
			
			CostProject model = new ProjectAction().findPlantByCode(mmPlantInb.getPlantCode());
			if(model == null)
			{
				model = new CostProject();
				
				//Set Id
				CostProjectPK costProjectPK = new CostProjectPK();
				costProjectPK.setCompany(company.getCompany());
				costProjectPK.setProject(mmPlantInb.getProjectCode());
				model.setId(costProjectPK);
				
				model.setProjectName(mmPlantInb.getPlantDescri());
				model.setSapPlantCode(mmPlantInb.getPlantCode());
				model.setStatus("P");
				model.setSapCompanyCode(mmPlantInb.getCompanyCode());
				
				model.setSyncExternalRefKey(mmPlantInb.getExtRefKey());
				model.setSyncExternalRefNo(mmPlantInb.getExtRefNo());
				
				model.setSyncIssync("N");
				model.setCreatedOn(Utilitys.ConvertStringToDateTime(mmPlantInb.getCreatedOn()));
				model.setCreatedBy(mmPlantInb.getCreatedBy());
				
				projSrv.Inserted(model);
				return FindByKey(company.getCompany(), mmPlantInb.getProjectCode());
				
			}else{
				
				if(model.getStatus().equals("P"))
				{
					log.info(" Project is not null ");
					
					model.setProjectName(mmPlantInb.getPlantDescri());
					model.setSapPlantCode(mmPlantInb.getPlantCode());
					model.setSapCompanyCode(mmPlantInb.getCompanyCode());
					
					model.setSyncExternalRefKey(mmPlantInb.getExtRefKey());
					model.setSyncExternalRefNo(mmPlantInb.getExtRefNo());
					
					model.setSyncIssync("N");
					
					if(CommonService.IsNull(mmPlantInb.getUpdatedOn()))
						model.setLastUpdOn(Utilitys.ConvertStringToDateTime(mmPlantInb.getCreatedOn()));				
					else
						model.setLastUpdOn(Utilitys.ConvertStringToDateTime(mmPlantInb.getUpdatedOn()));
					
					model.setLastUpdBy(CommonService.IsNullRNull(mmPlantInb.getCreatedBy()));
					
					projSrv.Updated(model);
					
					return FindByKey(company.getCompany(), mmPlantInb.getProjectCode());
				}
				else if(model.getStatus().equals("A"))
					throw new Exception(" This Project = "+ mmPlantInb.getPlantCode() + " // is " + model.getStatus() + "= Actived");
				
			}
		}catch(Exception e){
			log.error(e.getMessage());
			System.err.println(e.getMessage());
			throw e;
		}
		return null;
	}
	
	/**
	 * 
	 * @param lst
	 * @throws Exception
	 */
	public void CreateToPromis(List<MmPlantInb> lst) throws Exception 
	{
		try{
			
			ProjectAction xx =new ProjectAction();
			MmPlantInbServices objPlant= new MmPlantInbServices(sessionFactory.CreateEmSynchro());
			
			if(!CommonService.IsNull(lst)){
				for(MmPlantInb mmPlantInb : lst){
					log.info(mmPlantInb.getPromisStagingId());
					try{
						
						xx.SaveOrUpdated(mmPlantInb);
						
						mmPlantInb.setIssync("Y");
						mmPlantInb.setMsgCode("S");
						mmPlantInb.setMsgDesc("Success");
						mmPlantInb.setMsgActResult(null);
						objPlant.Updated(mmPlantInb);
						
					}catch(Exception e){
						
						mmPlantInb.setIssync("Y");
						mmPlantInb.setMsgCode("E");
						mmPlantInb.setMsgDesc("ERROR");
						mmPlantInb.setMsgActResult(CommonService.CutString(e.getMessage(), 3500));
						objPlant.Updated(mmPlantInb);
						
						log.error(e);
					}
				}
			}
			
		}catch(Exception e)
		{		
			log.error(e.getMessage());
			System.err.println(e.getMessage());
			throw e;
		}
	}
	
	/**
	 * 
	 * Date =Nov 13, 2013 : 2:36:49 PM 
	 * CreateBy=Chatree Siangklom
	 * void
	 * TODO
	 * @throws Exception
	 */
	@Deprecated
	private void Tested() throws Exception {
		
		ProjectAction xx =new ProjectAction();
		MmPlantInbServices objPlant= new MmPlantInbServices(sessionFactory.CreateEmSynchro());		
		List<MmPlantInb> lst = objPlant.findAllIsSync("N");
		
		if(!CommonService.IsNull(lst)){
			for(MmPlantInb mmPlantInb : lst){
				log.info(mmPlantInb.getPromisStagingId());
				try{
					
					xx.SaveOrUpdated(mmPlantInb);
					
					mmPlantInb.setIssync("Y");
					mmPlantInb.setMsgCode("S");
					mmPlantInb.setMsgDesc("Success");
					mmPlantInb.setMsgActResult(null);
					objPlant.Updated(mmPlantInb);
					
				}catch(Exception e){
					mmPlantInb.setIssync("N");
					mmPlantInb.setMsgCode("E");
					mmPlantInb.setMsgDesc("ERROR");
					mmPlantInb.setMsgActResult(e.getMessage());
					objPlant.Updated(mmPlantInb);
					
					log.error(e.getMessage());
					System.err.println(e.getMessage());
				}
			}
		}
	}
	
	/**
	 * 
	 * Date =Nov 13, 2013 : 2:36:52 PM 
	 * CreateBy=Chatree Siangklom
	 * void
	 * TODO
	 * @param args
	 */
	public static void main(String args[]){
		
		ProjectAction xx =new ProjectAction();
		try {
			
			xx.Tested();
			
		} catch (Exception e1) {
			log.error(e1.getMessage());
			System.err.println(e1.getMessage());
		}
	}

}
